package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.DepartmentDao;
import com.po.Department;
import com.po.Person;
import com.util.DbUtil;

public class DepartmentDaoImpl implements DepartmentDao {

	@Override
	public int addDepartment(Department department) {
		String sql = "insert into Department(did,dname,city) values(Departmentseq.Nextval,?,?)";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		int m = 0;
		

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);

			st.setString(1, department.getdName());
			st.setString(2, department.getCity());

			m = st.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return m;
	}

	@Override
	public int updateDepartment(Department department) {
		String sql = "update Department set dname=?,city=? where did=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		int m = 0;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, department.getdName());
			st.setString(2, department.getCity());
			st.setInt(3, department.getDid());
			m = st.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return m;
	}

	@Override
	public int deleteDepartment(int did) {
		String sql = "DELETE FROM Department where did=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		int m = 0;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setInt(1, did);
			m = st.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return m;
	}

	@Override
	public Department loadDepartment(int did) {
		String sql = "select * from Department  where did=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		Department d = null;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setInt(1, did);
			rs = st.executeQuery();
			if (rs.next()) {
				d = new Department(rs.getString("dName"), rs.getString("city"));
				d.setDid(rs.getInt("did"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return d;
	}

	@Override
	public List<Department> showAllDepartment() {
		String sql = "select * from Department";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		List<Department> departments = new ArrayList<Department>();

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			rs = st.executeQuery();
			while (rs.next()) {
				Department d = new Department(rs.getString("dName"), rs.getString("city"));
				d.setDid(rs.getInt("did"));
				departments.add(d);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return departments;
	}

	
}
